21. Quiz: Percentiles
Percentiles with Partitions
You can use partitions with percentiles to determine the percentile of a specific subset of all rows. Imagine you're an analyst at Parch & Posey and you want to determine the largest orders (in terms of quantity) a specific customer has made to encourage them to order more similarly sized large orders. You only want to consider the
NTILE
for that customer's
account_id
.
In the SQL Explorer below, write three queries (separately) that reflect each of the following:
-
Use the
NTILEfunctionality to divide the accounts into 4 levels in terms of the amount ofstandard_qtyfor their orders. Your resulting table should have theaccount_id, theoccurred_attime for each order, the total amount ofstandard_qtypaper purchased, and one of four levels in astandard_quartilecolumn.
-
Use the
NTILEfunctionality to divide the accounts into two levels in terms of the amount ofgloss_qtyfor their orders. Your resulting table should have theaccount_id, theoccurred_attime for each order, the total amount ofgloss_qtypaper purchased, and one of two levels in agloss_halfcolumn.
-
Use the
NTILEfunctionality to divide the orders for each account into 100 levels in terms of the amount oftotal_amt_usdfor their orders. Your resulting table should have theaccount_id, theoccurred_attime for each order, the total amount oftotal_amt_usdpaper purchased, and one of 100 levels in atotal_percentilecolumn.
Note: To make it easier to interpret the results, order by the account_id in each of the queries.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: sql-evaluator
- Opened files (when workspace is loaded): n/a